const app = getApp();
let gotoAppid = "wx83ed21db6ad34120"

Page({
  data: {
    integral: 0,
    system:{},
  },
  onShow() {
    this.showMyjinbiNum() //动态显示积分数量
    if (interstitialAd) { // 在适合的场景显示插屏广告
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },
  onLoad() {
    this.createInterstitialAd(app.globalData.system.chaping_ad) //创建插屏广告实例
    // console.log(app.globalData.system, 'my--------------');
    // console.log(app.globalData.system.chaping_ad, 'my--------------');
    this.setData({
      system : app.globalData.system
    })
    this.getGotoAppid()
  },

  //创建插屏广告实例
  createInterstitialAd(adUnitId) {
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
    }
  },

  // 兑换
  duihuan() {
    console.log(this.data.integral);
    if (this.data.integral < 300) {
      wx.showToast({
        icon: 'none',
        title: '满300积分才能兑换',
      })
      return
    }
    wx.navigateTo({
      url: `/pages/my_tixian/my_tixian?jinbi=${this.data.integral}`,
    })
  },
  //动态显示积分数量
  showMyjinbiNum() {
    const db = wx.cloud.database()
    db.collection('user').where({
        _openid: app.globalData.openid
      }).field({
        integral: true
      })
      .get()
      .then(res => {
        this.setData({
          integral: res.data[0].integral
        })
      })
      .catch(console.error)
  },

  // https://www.fendou.life/api/common/appid
  getGotoAppid() {
    if(!this.data.system.more_app){
      return
    }
    wx.request({
      url: 'https://www.fendou.life/api/common/appid',
      success: res => {
        gotoAppid = res.data[0].appid
      }
    })
    // console.log('执行了愤豆网查询');
  },
  // 更多积分平台
  moreJinbi() {
    // console.log(app.globalData.gotoAppid.appid,'=============');
    // 获取统一跳转的小程序appid
    
    wx.navigateToMiniProgram({
      appId: gotoAppid,
      path: 'page/index/index?tag=moreJinbi',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },

})